package com.xjscrm.console.chat.mq.consumer;

import com.alibaba.fastjson.JSON;
import com.uzai.mobile.collect.api.MicaTopics;
import com.uzai.mobile.collect.api.dto.MicaTransDto;
import com.uzai.mobile.collect.api.enums.MicaMsgType;
import com.uzai.trace.TraceGenerate;
import com.xjscrm.console.chat.mq.MsgHandler;
import com.xjscrm.console.chat.utils.ChatTalkMsgHandlerHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/12 14:10
 */
@RocketMQMessageListener(topic = MicaTopics.CHAT_TALK_TOPIC, consumerGroup = MicaTopics.CHAT_TALK_TOPIC, messageModel = MessageModel.CLUSTERING)
@Component
@Slf4j
public class MsgConsumer implements RocketMQListener<MicaTransDto> {

    @Override
    @TraceGenerate
    public void onMessage(MicaTransDto message) {

        log.info("收到MQ消息: {}", JSON.toJSONString(message));

        Integer msgType = message.getMsgType();
        MicaMsgType byValue = MicaMsgType.getByValue(msgType);

        MsgHandler<?> talkMsgHandler = ChatTalkMsgHandlerHolder.matchProcessor(msgType);

        if (Objects.nonNull(talkMsgHandler)) {
            talkMsgHandler.handle(message);
        } else {
            log.error("消息展示未做处理: {}", byValue.getDesc());
        }
    }
}
